package zhibi.cms.controller.admin;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.cms.domain.Menu;
import zhibi.cms.domain.WebPage;
import zhibi.cms.extra.base.AdminBaseController;
import zhibi.cms.extra.context.Constant;
import zhibi.cms.extra.pojo.WebPagePo;
import zhibi.cms.service.MenuService;
import zhibi.cms.service.WebPageService;
import zhibi.commons.exception.MessageException;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(Constant.SYS_PREFIX + "/admin/webpage")
public class CmsAdminWebPageController extends AdminBaseController {

    @Autowired
    private WebPageService webPageService;
    @Autowired
    private MenuService menuService;

    /**
     * 列表
     *
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Page page, WebPagePo webPagePo) {
        Example example = Example.getInstance()
                .addParam("w.title", webPagePo.getTitle(), ExampleType.Operation.LIKE)
                .addParam("m.name", webPagePo.getMenuName(), ExampleType.Operation.LIKE);
        PageInfo<WebPagePo> pageInfo = webPageService.selectPoByExample(example, page);
        setModelAttribute(model, pageInfo);
        return Constant.SYS_PREFIX + "/webPage/list";
    }

    /**
     * 到添加页面
     *
     * @param model
     * @return
     */
    @GetMapping("add")
    public String add(Model model) {
        List<Menu> menuList = menuService.selectAll();
        model.addAttribute("menuList", menuList);
        return Constant.SYS_PREFIX + "/webPage/add";
    }

    /**
     * 添加文章
     *
     * @return
     */
    @PostMapping("add")
    public String add(WebPage webPage) {
        webPage.setAddtime(new Date());
        webPageService.insertSelective(webPage);
        return redirect("list");
    }

    /**
     * 详情
     *
     * @param model
     * @param id
     * @return
     */
    @GetMapping("edit/{id}")
    public String edit(Model model, @PathVariable String id) {
        WebPage webPage = webPageService.selectByPrimaryKey(id);
        if (null == webPage) throw new MessageException("该网页不存在");
        List<Menu> menuList = menuService.selectAll();
        model.addAttribute("menuList", menuList);
        model.addAttribute("webPage", webPage);
        return Constant.SYS_PREFIX + "/webPage/edit";
    }

    /**
     * 更新
     *
     * @param webPage
     * @return
     */
    @PostMapping("update")
    public String update(WebPage webPage) {
        webPageService.updateByPrimaryKeySelective(webPage);
        return refresh();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable String id) {
        WebPage webPage = webPageService.selectByPrimaryKey(id);
        if (null == webPage) throw new MessageException("该网页不存在");
        webPageService.delete(webPage);
        return refresh();
    }

}
